//
//  PaperAssessmentViewController.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/1/12.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAssessmentViewController.h"
#import "NALLabelsMatrix.h"
#import "PaperAssessmentCollectionView.h"
#import "JiGuangUnit.h"

#import "PaperViewControllerBuild.h"

@interface PaperAssessmentViewController () <didSelectCollectionDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIView *titleLine1;
@property (weak, nonatomic) IBOutlet UIView *titleLine2;
@property (weak, nonatomic) IBOutlet UIView *titleLine3;
@property (weak, nonatomic) IBOutlet NALLabelsMatrix *matrixView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *matrixViewHeight;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property (weak, nonatomic) IBOutlet UIView *wrongView;
@property (weak, nonatomic) IBOutlet UIView *notView;
@property (weak, nonatomic) IBOutlet PaperAssessmentCollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *examName;
@property (weak, nonatomic) IBOutlet UILabel *examTime;

@property (weak, nonatomic) IBOutlet UILabel *examScore;
@property (weak, nonatomic) IBOutlet UILabel *examUserScore;
@property (weak, nonatomic) IBOutlet UILabel *examUserTime;
@property (weak, nonatomic) IBOutlet UILabel *examUserCount;

@property (weak, nonatomic) IBOutlet UILabel *examAllCount;
@property (weak, nonatomic) IBOutlet UILabel *examAvgScore;
@property (weak, nonatomic) IBOutlet UILabel *examAvgTime;
@property (weak, nonatomic) IBOutlet UILabel *examBeatCount;

@property (weak, nonatomic) IBOutlet UILabel *examBigScore;

@property (weak, nonatomic) IBOutlet UIButton *allAnaly;
@property (weak, nonatomic) IBOutlet UIButton *errAnaly;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;

@end

@implementation PaperAssessmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //套卷 评估界面
    [self setupConfigureView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)headRightAction{
    
    
//    JSHAREMessage *message = [JSHAREMessage message];
//    message.mediaType = JSHARELink;
//    NSDictionary *dic = @{@"share_type":@"9",@"share_id":self.model.report_id};
//    [[JiGuangUnit sharedInstant] shareViewAddSubview:self.view withMessage:message andDic:dic];
    
}

- (void)setupConfigureView {
//    self.title = @"试卷评估";
//    [self loadHeadView];
    [self loadHeadView:@"答题报告" addRightBtn:YES rightImageName:@"CourseDetailShareIcon" rightTitle:nil];
    if (kDevice_Is_iPhoneX) {
        self.topConstraint.constant = kNavigationBarHeight;
        self.bottomConstraint.constant = kBottomSafeHeight;
    }

    self.automaticallyAdjustsScrollViewInsets=NO;
    self.scrollView.hidden = YES;
    self.titleLine1.backgroundColor = MainThemeColor;
    self.titleLine2.backgroundColor = MainThemeColor;
    self.titleLine3.backgroundColor = MainThemeColor;
    
    self.wrongView.backgroundColor = RGB(264, 54, 60);
    ViewBorderRadius(self.wrongView, 8.0, 1.0, RGB(264, 54, 60));
    
    self.rightView.backgroundColor = MainThemeColor;
    ViewBorderRadius(self.rightView, 8.0, 1.0, MainThemeColor);
    ViewBorderRadius(self.notView, 8.0, 1.0, RGB(234, 234, 234));
    
    self.allAnaly.backgroundColor = MainThemeColor;
    self.errAnaly.backgroundColor = MainThemeColor;
    
//    NSNumber *columnsWidth = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 3.0];
//    self.matrixView.columnsWidths = @[columnsWidth, columnsWidth, columnsWidth];
    NSNumber *columnsWidth = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 3.0];

    NSNumber *columnsWidth1 = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 3.0+20];
    NSNumber *columnsWidth2 = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 3.0-10];
    NSNumber *columnsWidth3 = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 3.0-10];
    
    self.matrixView.columnsWidths = @[columnsWidth1, columnsWidth2, columnsWidth3];

    
//    NSNumber *columnsWidth = [NSNumber numberWithFloat:(SCREEN_WIDTH - 24.0f) / 4.0];
//    self.matrixView.columnsWidths = @[columnsWidth, columnsWidth, columnsWidth, columnsWidth];
    
    if (![self.backViewController isEqualToString:@"PracticeRecordViewController"]) {
        // 答题记录，加载过来的，不需要刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateUserAnswerNotification" object:nil];
        if ([self.backViewController isEqualToString:@"SimulateSignalExamViewController"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateSimulateExamNotification" object:nil];
        }
    }
    
    if (self.model) {       // 直接展示
        self.paper_id = self.model.paper_id;
        self.report_id = self.model.report_id;
        [self updateViewData];
    } else {                // 加载数据展示
        self.viewModel = [[BaseViewModel alloc] initWithUrl:@"Question/tjbg"];
        self.viewModel.params.body = @{@"report_id" : self.report_id ? : @"",
                                            @"paper_id" : self.paper_id ? : @""};
        @weakify(self)
        [self didViewModelWithCompletionBlock:^(id receive) {
            @strongify(self)
            if ([receive isKindOfClass:[PaperAssessmentModel class]]) {
                self.model = receive;
                [self updateViewData];
            }
        }];
        [self.viewModel.command execute:@"PaperAssessmentModel"];
    }
    
    self.collectionView.didDatagate = self;
    self.collectionView.modelClass = @"PaperAssessmentCardModel";
    self.collectionView.minInteritemSpace = SCREEN_WIDTH / 4 - 56;
    self.collectionView.minLineSpace = 16.0f;
    self.collectionView.edgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, 16.0f, 0.0f);
}

- (void)updateViewData {
    self.scrollView.hidden = NO;
    self.examName.text = self.model.paper_name;
    self.examTime.text = self.model.datatime;
    
    self.examScore.text = [NSString stringWithFormat:@"%@分", self.model.paper_score];;
    self.examUserScore.text = [NSString stringWithFormat:@"%@分", self.model.user_score];;
    self.examUserTime.text = self.model.r_time_long;
    self.examUserCount.text = [NSString stringWithFormat:@"%@/%@道", self.model.answer_quantity, self.model.ques_count];
    
    self.examAllCount.text = [NSString stringWithFormat:@"%@人", self.model.answer_number];
    self.examAvgScore.text = [NSString stringWithFormat:@"%@分", self.model.average_score];
    self.examAvgTime.text = self.model.average_time;
    self.examBeatCount.text = [NSString stringWithFormat:@"%@人",self.model.beat];
    
    self.examBigScore.text = self.model.user_score;
    
    
    [self.model.matrixData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.matrixView addRecord:obj];
    }];
    self.matrixViewHeight.constant = self.matrixView.height;
    
    self.collectionView.sourceData = self.model.model_question;
    self.collectionViewHeight.constant = self.model.collectHeight;
}

- (void)headLeftAction {
    if (self.backViewController == nil) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        __block BOOL isexist = NO;
        NSArray *vcs = self.navigationController.viewControllers;
        [vcs enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:NSClassFromString(self.backViewController)]) {
                isexist = YES;
                [self.navigationController popToViewController:obj animated:YES];
                *stop = YES;
            }
        }];
        
        if (isexist == NO) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
}

#pragma mark - capabilityAssessmentDelegate
// 点击单个解析
- (void)collectionView:(UICollectionView *)collectionView didSelectIndexPath:(NSIndexPath *)indexPath {
    PaperAssessmentCardModel *model = self.collectionView.sourceData[indexPath.section];
    PaperAssessmentCardAnswerModel *answerModel = model.ques_data[indexPath.row];
    
    PaperViewController *viewController = [PaperViewControllerBuild createPastYearSignalAnalyViewControllerWithData:@{@"report_id": self.report_id ? : @"", @"ques_id" : answerModel.ques_id ? : @""}];
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - btn action

// 点击所有解析
- (IBAction)didAllAnalyAction:(id)sender {
    PaperViewController *viewController = [PaperViewControllerBuild createPastYearAllAnalyViewControllerWithData:@{@"report_id": self.report_id ? : @"", @"paper_id" : self.paper_id ? : @""}];
    [self.navigationController pushViewController:viewController animated:YES];
}

// 点击错误解析
- (IBAction)didWrongAnalyAction:(id)sender {
    PaperViewController *viewController = [PaperViewControllerBuild createPastYearWrongAnalyViewControllerWithData:@{@"report_id": self.report_id ? : @"", @"paper_id" : self.paper_id ? : @""}];
    [self.navigationController pushViewController:viewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
